package zua.program.androidstudioprogram01;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class DemoDatabase extends SQLiteOpenHelper {
    private static final String table_name = "demo01";
    private static final String column_username = "username";
    private static final String column_petName = "petName";
    private static final String column_password = "password";
    private static final int column_score = 0;
    private static String DBname = "myapplication";
    private static int Version = 1;



    //构造方法  , @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version
    public DemoDatabase(@Nullable Context context) {
        super(context, DBname, null, Version);//上下文，数据库名，游标，版本号
    }


    @Override //建立数据库 onCreate方法只执行一次
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "create table demo01(username varchar(20) primary key,petname varchar(20),password varchar(20),score int)";
        sqLiteDatabase.execSQL(sql);//建立数据库的同时建立数据表
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public String addData(Student student) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(column_username,student.getUsername());
        contentValues.put(column_petName,student.getPetName());
        contentValues.put(column_password,student.getPassword());
        contentValues.put(String.valueOf(column_score),student.getScore());

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        long insert = sqLiteDatabase.insert(table_name,column_username,contentValues);//表名，字段，值
        if (insert == -1) {
            return "fail";
        }
        sqLiteDatabase.close();
        return "success";
    }

    public String delete(Student student) {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        int delete = sqLiteDatabase.delete(table_name, column_username + "=?", new String[]{student.getUsername()});
        if (delete == 0) {
            return "fail";
        }
        sqLiteDatabase.close();
        return "success";
    }

    public String updateData(Student student) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(column_username,student.getUsername());
        contentValues.put(column_petName,student.getPetName());
        contentValues.put(column_password,student.getPassword());
        contentValues.put(String.valueOf(column_score),student.getScore());

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        int update = sqLiteDatabase.update(table_name,contentValues,column_username+"=?",new String[]{student.getUsername()});
        if (update == 0) {
            return "fail";
        }
        sqLiteDatabase.close();
        return "success";
    }

    public List<Student> selectDate() {
        List<Student> list = new ArrayList<>();
        Student student;
        String sql = "select * from " + table_name;

        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery(sql,null);

        int username_index = cursor.getColumnIndex(column_username);
        int petname_index = cursor.getColumnIndex(column_petName);
        int password_index = cursor.getColumnIndex(column_password);
        int score_index = cursor.getColumnIndex(String.valueOf(column_score));
        while (cursor.moveToNext()) {
            student = new Student(cursor.getString(username_index),cursor.getString(petname_index),cursor.getString(password_index),score_index);
            list.add(student);
        }
        cursor.close();
        sqLiteDatabase.close();
        return list;
    }
}
